package com.wang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.entity.TbBbsAttention;
import com.wang.entity.TbUser;
import com.wang.mapper.TbBbsAttentionMapper;
import com.wang.mapper.TbUserMapper;
import com.wang.response.ResultResponse;
import com.wang.service.TbBbsAttentionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *关注
 */
@Service
public class TbBbsAttentionServiceImpl implements TbBbsAttentionService{
    @Resource
    private TbBbsAttentionMapper tbBbsAttentionMapper;
    @Resource
    private TbUserMapper tbUserMapper;

    /*
    添加关注
     */
    @Override
    public ResultResponse insertbbsattention(TbBbsAttention tbBbsAttention) {
        int insert = tbBbsAttentionMapper.insert(tbBbsAttention);
        if (insert>0){
            return ResultResponse.SUCCESS();
        }else{
            return ResultResponse.FAIED(500,"添加失败");
        }
    }

    /**
     * 取消关注
     * @param
     * @return
     */
    @Override
    public ResultResponse removebbsattention(Integer attentionId) {
        int i = tbBbsAttentionMapper.deleteById(attentionId);
        if (i>0){
            return ResultResponse.SUCCESS();
        }else{
            return ResultResponse.FAIED(500,"删除失败");
        }
    }

    @Override
    public ResultResponse findbbsattention(Integer userId) {
        QueryWrapper<TbBbsAttention> QueryWrapper = new QueryWrapper<TbBbsAttention>();
        QueryWrapper.eq("user_id", userId);
        List<TbBbsAttention> bbsAttentions = tbBbsAttentionMapper.selectList(QueryWrapper);
        ArrayList list = new ArrayList();
        for (TbBbsAttention bbsAttention : bbsAttentions) {
            //被关注id
            Integer author = bbsAttention.getAuthorId();
            QueryWrapper<TbUser> tbUserQueryWrapper = new QueryWrapper<>();
            tbUserQueryWrapper.eq("user_id", author);
            List<TbUser> tbUsers = this.tbUserMapper.selectList(tbUserQueryWrapper);
            for (TbUser tbUser : tbUsers) {
                String userName = tbUser.getUserName();
                list.add(userName);
            }
        }
      return ResultResponse.SUCCESS(list);
    }


    /*
    查看所有的关注
     */
    @Override
    public ResultResponse findbbsattentionall(TbBbsAttention tbBbsAttention) {
        Page<TbBbsAttention> page = new Page<>(tbBbsAttention.getPagenum(), tbBbsAttention.getPagesize());
        QueryWrapper<TbBbsAttention> wrapper = new QueryWrapper<>();
        Page<TbBbsAttention> selectPage = tbBbsAttentionMapper.selectPage(page, wrapper);
        long total = selectPage.getTotal();
        return ResultResponse.SUCCESS(selectPage.getRecords(),total);
    }
}




